//
// Created by sjjmw on 3/24/2022.
//
#include "iostream"
#include "vector"
#include "list"
#include "algorithm"
#include "string"

//从低位累加到高位即可
char get_char_value(char c){
    if(c>='0'&&c<='9'){
        return c-'0';
    }
    return c-'A'+10;
}

void handle(int n,std::string&str){
    int sum=0;
    for(int i=0;i<str.size();i++){
        auto c=str[i];
        sum*=n;
        sum+= get_char_value(c);
    }
    printf("%d\n",sum);
}

int main(){
    int a,b;
    std::string str;
    while(std::cin>>a>>str){
        handle(a,str);
    }
    return 0;
}